<?php
//editAssignment.php
//provides the form an instructor can use to modify an assignment - rjs
$allow=true;

require_once("overhead.php");
if ($currentRole != "instructor") {
	throwError(2);
}
$crumb["link"]="index.php?cid=".$currentCourse."&uid=".$currentUser."&role=".$currentRole;
$crumb["title"]="Course Home";
$breadcrumbs[]=$crumb;
$pageTitle="Edit Assignment";
//Get the ID from the url
$assignmentNumber = (isset($_GET['aid']) ? $_GET['aid'] : 0);
if (isset($_REQUEST['submitted'])) {
	if (isset($_POST['assignmentTitle']) && isset($_POST['assignmentDescription'])) {

	$createAssignmentQuery=mysql_query("update assignment set Name='".mysql_real_escape_string($_POST['assignmentTitle'])."',Description='".mysql_real_escape_string($_POST['assignmentDescription'])."',Status='".($_POST['status']=='Open' ? 'Open' : 'Closed')."' where ID=".$_POST['aid']);
	if ($createAssignmentQuery) {
	header("location:viewAssignment.php?cid=".$currentCourse."&uid=".$currentUser."&role=".$currentRole."&msg=Assignment successfully modified.&aid=".$_POST['aid']);
	exit;
	} else { $error="<font color='red'>Failed.</font><br />"; }
}
}

//Get the assignment requested
$getAssignmentData = mysql_query("SELECT * FROM assignment WHERE ID = " .$assignmentNumber);
$assignmentData = null;
while ($row=mysql_fetch_assoc($getAssignmentData)) {
	$assignmentData['ID'] = $row['ID'];
	$assignmentData['CourseID'] = $row['CourseID'];
	$assignmentData['JUnit Test'] = $row['JUnit Test'];
	$assignmentData['Status'] = $row['Status'];
	$assignmentData['Description'] = $row['Description'];
	$assignmentData['Name'] = $row['Name'];
}
?>
<html>
      	<head>
              	<title>Edit an Assignment</title>
        </head>
        <body>
			<?php printBreadcrumbs(); ?>
              	<h2>Edit "<?php print $assignmentData['Name']; ?>"</h2>
              	<?php print $error; ?>
                <form method='post'>
                        Assignment name:<br /><input type='text' name='assignmentTitle' value='<?php print $assignmentData['Name']; ?>'/><br />
                        Open? <input type='checkbox' name='status' value='Open' <?php print ($assignmentData['Status']=='Open' ? "checked" : "" ); ?> ><br />
                        Assignment Description:<br />
                        <textarea name='assignmentDescription' style='width:100%;' rows='15'><?php print $assignmentData['Description']; ?></textarea>
						<input type='hidden' name='submitted' value='1' />
						<input type='hidden' name='aid' value='<?php print $assignmentNumber; ?>' />
                        <input type='submit' value='Save changes'>
                </form>
        </body>
</html>
